package com.zk.algorithm.linkedlist;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.Easy;
import com.zk.algorithm.annotation.LeetCodeExplore;
import com.zk.algorithm.bean.ListNode;
import com.zk.algorithm.factory.ListFactory;

/**
 * @author zk
 */
@LeetCodeExplore
@Easy
public class MergeTwoSortedLists {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode index = dummy;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                index.next = l1;
                l1 = l1.next;
            } else {
                index.next = l2;
                l2 = l2.next;
            }

            index = index.next;
        }

        if (l1 != null) {
            index.next = l1;
        } else if (l2 != null) {
            index.next = l2;
        }

        return dummy.next;
    }

    public static void main(String...args) {
        Utils.println(new MergeTwoSortedLists().mergeTwoLists(
                ListFactory.create(new int[]{1, 2, 4}),
                ListFactory.create(new int[]{1, 3, 4})
        ));
    }

}
